#include <stdio.h>
#include "ofc/DPropertyTree.h"
#include "ofc/DFile.h"
#include "ofc/DText.h"
#include "ofc/DInt.h"
#include "ofc/DBool.h"

int main(int argc, char *argv[])
{
#ifdef HAVE_DPROPERTYTREE
  DPropertyTree *tree = [DPropertyTree new];
  DProperty     *group;
  DFile         *file = [DFile new];

  DText         *stringValue = [DText new];  // Property value destinations
  DInt          *intValue    = [DInt  new];
  DBool         *boolValue   = [DBool new];

  char           name[]      = "property.xml";  
                                             // Build the property tree
  printf("Building the property tree.\n");
  group = [tree group :nil :"Example"];      //   First: Add a group to the root

  [stringValue set :"ExampleString"];        //   Set defaults to the values
  [intValue    set :10];
  [boolValue   set :YES];
                                             //   Second: Add the properties to the group
  [tree property :group :"string" :stringValue];
  [tree property :group :"int"    :intValue   ];
  [tree property :group :"bool"   :boolValue  ];
                                             // Write the defaults to a property file
  printf("Saving property tree to \"%s\".\n", name);
  if ([file open :name :"w"])
  {
    if (![tree write :file :name])
      printf("Could not write \"%s\".\n", name);

    [file close];
  }
  else
    printf("Could \"%s\" not open for writing:%d\n", name, [file error]);

                                       // Test: reset the properties
  printf("Reset the properties.\n");
  [stringValue set :""];
  [intValue    set :0 ];
  [boolValue   set :NO];
                                       // Read the property file
  printf("Read the properties back from \"%s\".\n", name);
  if ([file open :name :"r"])
  {
    if (![tree read :file :name])
      printf("Could not read \"%s\".\n", name);

    [file close];
  }
  else 
    printf("Could \"%s\" not open for reading:%d\n", name, [file error]);

  printf("Property \"string\":%s\n", [stringValue cstring]);
  printf("Property \"int\"   :%d\n", [intValue get]);
  printf("Property \"bool\"  :%d\n", [boolValue get]);

  [tree free];                         // Cleanup
  [file free];
#else
  printf("DPropertyTree is not present in the library.\n");
#endif
  return 0;
}

